package edu.ucsd.ccdb.util;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;

import edu.ucsd.ccdb.MainConfiguration;
import edu.ucsd.ccdb.servlet.ServletSendReceive;
import edu.ucsd.irods.IRODSFileString;
import edu.ucsd.irods.IRODSOperation;

public class TreeOperation {

	public static void createNodes ( DefaultMutableTreeNode top ) throws NullPointerException, IOException {
		
		IRODSFileString file = (IRODSFileString) top.getUserObject();
		
		if ( MainConfiguration.DEBUG){
			System.out.println ("All files: " + IRODSOperation.getListFiles( "/CCDBProdZone/home/CCDB_DATA_USER/acquisition/project_10011/microscopy_1/" ));
		}
		
		// query
		String query = MainConfiguration.SERVLET_SERVER + MainConfiguration.SERVLET_PARAMETER + URLEncoder.encode( file.getAbsolutePath(), "UTF-8" );
		
//		List<IRODSFileString> listFiles = ServletSendReceive.getResponse( MainConfiguration.QUERY_TEST );
		List<IRODSFileString> listFiles = ServletSendReceive.getResponse( query );
		
		if (listFiles == null )
			return;
		
		// process 
		for (IRODSFileString element: listFiles) {			
			DefaultMutableTreeNode node = new DefaultMutableTreeNode(element);
			top.add(node);			
		}
	}
}
